<template>
  <div class="app-container">
    <!-- 添加或修改生产报工记录对话框 -->
    <el-dialog :title="title" v-if="printOpen" :visible.sync="printOpen" :modal="false" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="条码规则" prop="ruleId">
              <el-select v-model="form.ruleId" placeholder="请输入条码规则" :disabled="form.printQuantity > 0">
                <el-option
                  v-for="item in ruleList"
                  :key="item.ruleId"
                  :label="item.ruleCode"
                  :value="item.ruleId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次数量" prop="batchQuantity">
              <el-input-number v-model="form.batchQuantity" :min="0" :max="form.quantity - form.printQuantity" placeholder="请输入批次数量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排产数量" prop="quantity">
              <el-input v-model="form.quantity" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已打印数量" prop="printQuantity">
              <el-input v-model="form.printQuantity" :min="0" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单编号" prop="workorderCode">
              <el-input v-model="form.workorderCode" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序名称" prop="processName">
              <el-input v-model="form.processName" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品物料名称" prop="itemName">
              <el-input v-model="form.itemName" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <Barcode-Print ref="barcodeprint" :genCode="genCode"></Barcode-Print>
  </div>
</template>

<script>
import {addLabel, getLabelInfo, updateLabel} from "@/api/mes/pro/prolabel";
import {getList} from "@/api/mes/md/barCodeItem";
import BarcodePrint from "@/components/barcodeprint/index.vue";

export default {
  name: "proLabel",
  components: {BarcodePrint},
  data() {
    return {
      genCode: null,
      //车间选项
      ruleList: [],
      // 弹出层标题
      title: "批次标签打印",
      // 是否显示弹出层
      printOpen: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ruleId: [
          {required: true, message: "条码规则不能为空", trigger: "blur"}
        ], batchQuantity: [
          {required: true, message: "批次数量不能为空", trigger: "blur"}
        ],
      },
      // 查询参数
      queryParams: {
        itemId: null,
      },
    };
  },

  methods: {
    /** 查询生产标签列表 */
    init(taskId) {
      getLabelInfo(taskId).then(response => {
        if (response.code == 200 && response.data) {
          this.form = response.data;
          this.queryParams.itemId = this.form.itemId;
          getList(this.queryParams).then(response => {
            this.ruleList = response.data;
          });
          this.printOpen = true;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.printOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        labelCode: null,
        ruleId: null,
        ruleCode: null,
        ruleName: null,
        taskId: null,
        taskCode: null,
        taskName: null,
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        routeId: null,
        routeCode: null,
        processId: null,
        processCode: null,
        processName: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantity: null,
        batchQuantity: null,
        printQuantity: 0,
        status: "0",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLabel(this.form).then(response => {
              if (response.code == 200) {
                this.genCode = response.msg;
                this.printOpen = false;
                this.$refs.barcodeprint.showFlag = true;
                this.$refs.barcodeprint.title = '条码打印';
              }

            });
          } else {
            addLabel(this.form).then(response => {
              if (response.code == 200) {
                this.genCode = response.msg;
                this.printOpen = false;
                this.$refs.barcodeprint.showFlag = true;
                this.$refs.barcodeprint.title = '条码打印';
              }
            });
          }
        }
      });
    },
  }
};
</script>
